package com.feihong.txgw.ui.activity.order;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.ProductOrderDetailAdapter;
import com.feihong.txgw.inter.contract.order.TernminalOrderDetailContract;
import com.feihong.txgw.json.user.ProductOrderDetailEntity;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.tx_precenter.TerminalOrderDetailPrecenter;
import com.feihong.txgw.ui.fragment.order.OrderListFragment;
import com.feihong.txgw.ui.mvp.BaseMVPCompateActivity;
import com.feihong.txgw.ui.mvp.precenter.AbBasePrecenter;

import java.util.List;

import butterknife.BindView;

/**
 * @param
 * @author wangziheng
 * @time 2017/8/16  19:18
 * @describe 终端订单详情
 */
public class ProductsOrderDetailsActivity extends BaseMVPCompateActivity<TernminalOrderDetailContract.AbTerminalOrderDetailMainPrecenter> implements View.OnClickListener, TernminalOrderDetailContract.ITerminalOrderDetailMainView {

    private String order_id;
    private int table_id;

    @BindView(R.id.productOrderDetailRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.lay_loading) RelativeLayout mLoadLayout;
    @BindView(R.id.connect_net_error_layout) LinearLayout mConnectError;
    @BindView(R.id.re_query_data_layout) LinearLayout mRequeryData;
    private LinearLayoutManager linearLayoutManager;

    private ProductOrderDetailAdapter mAdapter;

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_order_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        order_id = getIntent().getStringExtra("order_id");
        table_id = getIntent().getIntExtra("table_id", 0);
        linearLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ProductOrderDetailAdapter(this, refreshCallBack);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void bindData() {
        //设置页面标题
        setPageTitle("订单详情");
        //设置左上角关闭按钮的事件
        setBackButtonFinish();
        mRequeryData.setOnClickListener(this);
        getNet();
    }

    private void getNet() {
        mLoadLayout.setVisibility(View.VISIBLE);
        mPresenter.loadOrderDetail(String.valueOf(table_id),order_id);
    }

    OrderListFragment.OrderRefreshCallBack refreshCallBack = new OrderListFragment.OrderRefreshCallBack() {
        @Override
        public void refresh(int pagestate) {
            getNet();
        }
    };

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.re_query_data_layout:
                mConnectError.setVisibility(View.GONE);
                mLoadLayout.setVisibility(View.VISIBLE);
                mRecyclerView.setVisibility(View.GONE);
                getNet();
                break;
            default:
                break;
        }
    }

    @Override
    public void loadOrderDetailResult(List<ProductOrderDetailEntity> orderInfo) {

        mRecyclerView.setVisibility(View.VISIBLE);
        mConnectError.setVisibility(View.GONE);
        mAdapter.setGoods(orderInfo);
    }

    @Override
    public void loadDataComplated() {
        mLoadLayout.setVisibility(View.GONE);
    }

    @Override
    public void loadDataError() {
        mRecyclerView.setVisibility(View.GONE);
        mLoadLayout.setVisibility(View.GONE);
        mConnectError.setVisibility(View.VISIBLE);
    }

    @NonNull
    @Override
    public AbBasePrecenter initPresenter() {
        return TerminalOrderDetailPrecenter.create();
    }
}
